<template>
  <el-dialog
    :close-on-click-modal="false"
    title="电子签章记录"
    append-to-body
    width="50%"
    :visible.sync="showDialog"
  >
    <template #title>
      <div class="dialog-title">
        <div class="title">
          电子签章记录
        </div>
        <div class="right">
          <el-button type="primary" @click="mergeDownload">合并下载</el-button>
        </div>
      </div>
    </template>
    <page-table
      ref="multipleSelection"
      :page-data="pageData"
      :loading="loading"
      :do-get-page-data="getPageData"
      :autoData="true"
      @select-change="handleSelectionChange"
    >
      <el-table-column type="selection"> </el-table-column>
      <el-table-column prop="createUserName" label="签章人" width="80">
      </el-table-column>
      <el-table-column prop="createTime" label="签章时间"> </el-table-column>
      <el-table-column prop="signFilePath" label="签章文件">
        <template slot-scope="scope">
          <el-tooltip
            :content="getFileName(scope.row.signFilePath)"
            placement="top"
            ><el-button type="text" @click="look(scope.row)">
              {{ getFileName(scope.row.signFilePath) }}
            </el-button></el-tooltip
          >
        </template>
      </el-table-column>
      <el-table-column label="管理" width="160" align="center" fixed="right">
        <template slot-scope="scope">
          <el-tooltip content="下载" placement="top"
            ><el-button
              type="button"
              size="mini"
              icon="el-icon-download"
              @click="downLoad(scope.row)"
            >
            </el-button
          ></el-tooltip>
          <el-tooltip content="删除" placement="top"
            ><el-button
              type="danger"
              size="mini"
              icon="el-icon-delete"
              @click="remove(scope.row)"
            >
            </el-button
          ></el-tooltip>
        </template>
      </el-table-column>
    </page-table>
  </el-dialog>
</template>

<script>
import _ from "lodash";
import { previewFile } from "@/utils/previewOpen.js";
export default {
  name: "SignListDialog",

  props: {
    value: {
      type: [Boolean],
      default: false
    },
    queryData: {
      type: [Object],
      default: () => {
        return {};
      }
    }
  },

  data() {
    return {
      pageData: {},
      loading: false,
      showDialog: false,
      showSelect: false,
      selectRow: [],
      listQuery: {}
    };
  },

  watch: {
    showDialog(n) {
      if (this.value === n) {
        return;
      }
      this.$emit("input", n);
    },
    value(n) {
      if (this.showDialog === n) return;
      if (n) {
        this.getPageData();
      }
      this.showDialog = n;
    }
  },

  mounted() {},

  methods: {
    // 取文件名
    getFileName(path) {
      if (!path) {
        return "";
      }
      const arr = path.split("/");
      if (arr.length && arr.length > 1) {
        return arr[arr.length - 1];
      }
      return path;
    },
    // 获取列表
    getPageData(val) {
      const obj = _.cloneDeep(this.queryData);
      Object.assign(obj, this.listQuery);
      if (val) {
        this.listQuery = val;
        Object.assign(obj, val);
      }
      if (!obj.pageIndex) {
        return;
      }
      this.API.getSignPageList(obj).then(res => {
        this.pageData = res.data;
      });
    },
    // 查看详情
    look(row) {
      const { signFilePath } = row;
      previewFile(signFilePath);
    },
    // 下载
    downLoad(row) {
      const { id, signFilePath } = row;
      let fileDownload = require("js-file-download");
      this.ExportApi.getSignPdf({ ids: id }).then(res => {
        fileDownload(res.data, this.getFileName(signFilePath));
        this.$message({
          message: "下载成功",
          type: "success"
        });
      });
    },
    // 选框事件
    handleSelectionChange(val) {
      this.selectRow = val;
    },
    // 合并下载
    mergeDownload() {
      if (!this.selectRow.length) {
        this.$message("请选择记录");
        return;
      }
      let arr = this.selectRow.map(item => {
        return item.id;
      });
      let fileDownload = require("js-file-download");
      this.ExportApi.getSignPdf({ ids: arr.join(",") }).then(res => {
        fileDownload(res.data, "签章记录.pdf");
        this.$message({
          message: "下载成功",
          type: "success"
        });
      });
    },
    // 删除
    remove(row) {
      const { id } = row;
      this.$confirm("此操作将永久删除该记录, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.API.deleteWorkflowSignById({ id }).then(() => {
            this.$message("删除成功");
            this.getPageData();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    }
  }
};
</script>

<style lang="scss" scoped>
.dialog-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-right: 50px;
}
</style>
